// pages/pages-list/huizhan-details/huizhan-details.js
import api from '../../../utils/api/api'
import tool from "../../../utils/publics/tool"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isUseShare: true, // 局部分享
    id: '', // 会展id
    pink_id: '', // 拼团id
    details: {}, // 会展详情
    pinkDetail: {}, // 拼团详情
    btnType: '', // 按钮状态
    imgShow: false,
    active: ['注册', '查看入场劵', '拼团中', '审核中', '重新注册','去支付'],
    active2: ['参与拼团', ''],
    vShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, "详情页");
    tool.storage("id", options.id || '');
    if(options.scene){
      tool.storage("scene", options.scene);
    }
   
    this.setData({
      id: options.id || '1',
      pink_id:options.pink_id || ''
    });
    tool.loading();
    this.getData();

  },
  // 监听图片是否加载完
  bindload() {
    tool.loading_h();
    this.setData({
      imgShow: true,
    })
  },
  // 会展详情
  getData() {
    api.detail({
      id: this.data.id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res;
      if (code == 1) {
        this.setData({
          details: data
        });
        if (data.is_join == 0 && this.data.pink_id) {
          this.pink_detail();
        }else if(data.is_join == 1 && this.data.pink_id){
          this.setData({
            vShow:true
          })
        }
      }
    })
  },
  // 拼团详情
  pink_detail() {
    api.pink_detail({
      pink_id: this.data.pink_id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res;
      if (code == 1) {
        tool.storage("pink_id", this.data.pink_id || '');
        let vShow = false;
        if(data.pink_status==2 || data.pink_status==3){
          vShow=true
        }
        this.setData({
          pinkDetail: data,
          vShow
        })
      }
    })
  },
  // 收藏
  collection() {
    let details = this.data.details;
    api.collection({
      id: this.data.id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res;
      tool.alert(msg, code);
      if (code == 1) {
        details.is_collection = details.is_collection == 1 ? 0 : 1;
        this.setData({
          details
        })
      }
    })
  },
  // 跳转
  skip() {
    let details = this.data.details; //  会展详情 
    let pink_id = this.data.pink_id;
    let pinkDetail = this.data.pinkDetail; //拼团详情 pink_status 0未拼团 1拼团中 2拼团成功 3拼团失败 
    if (pinkDetail.pink_status == 0 || pinkDetail.pink_status == 1) {
      tool.jump_nav("/pages/pages-list/category/category");
      return
    } else if (pinkDetail.pink_status == 2) {
      tool.jump_nav("/pages/pages-list/category/category");
      tool.storage("pink_id", '');
      return
    }
    if (details.is_join == 0 || details.is_join == 4) { // 已报名 1已报名 0 未报名 2拼团中 3审核中 4审核拒绝   
      return tool.jump_nav("/pages/pages-list/category/category")
    } else if (details.is_join == 1) {
      if (details.pink_id) {
        return tool.jump_nav(`/pages/pages-list/group-details/group-details?id=${details.pink_id}`)
        // this.setData({
        //   vShow: true
        // })
      } else {
        return tool.jump_nav(`/pages/pages-list/success/success?id=${details.enroll_id}`)
      }
    } else if (details.is_join == 2) {
      return tool.jump_nav(`/pages/pages-list/group-details/group-details?id=${details.pink_id}`)
    } else if (details.is_join == 3) {
      return tool.alert("你的报名审核中")
    }else if(details.is_join == 5){      // 去支付
      return tool.jump_nav(`/pages/pages-list/my-activity/my-activity`)
    }
  },
  // 关闭弹框
  close() {
    let details = this.data.details;
    let pinkDetail = this.data.pinkDetail;
    let pink_id = this.data.pink_id;
    this.setData({
      vShow: false
    });
    return
    // 拼团成功 拼团失败 报名成功 
    if(details.is_join==1){
      return tool.jump_nav(`/pages/pages-list/success/success?id=${details.enroll_id}`)
    }else if(pinkDetail.pink_status==2 || pinkDetail.pink_status==3){
      tool.storage("pink_id", '');
      tool.jump_nav("/pages/pages-list/category/category");
    }
    return
    if (pink_id && details.is_join == 1) {
      tool.jump_nav(`/pages/pages-list/group-details/group-details?id=${details.pink_id}`)
    } else {
      tool.storage("pink_id", '');
      tool.jump_nav("/pages/pages-list/category/category");
    }

  },
  // 授权手机号回调
  getPhoneCallback(e) {
    if (e.detail.status) {
      api.detail({
        id: this.data.id
      }).then(res => {
        let {
          code,
          data,
          msg
        } = res;
        if (code == 1) {
          this.setData({
            details: data
          })
          this.skip();
        }
      })
    }
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let nickname = getApp().store.$state.userInfo.nickname;
    let details = this.data.details;
    let title = `${nickname}邀请您一起参加东莞AG潮人集`;
    if (details.is_join != 1) {
      title = '10月14-16日东莞AG潮人集'
    }
    let id = this.data.id;
    return {
      title,
      path: `/pages/pages-list/huizhan-details/huizhan-details?id=${id}&scene=${wx.getStorageSync("scene")||''}`,
      imageUrl: `https://img.vrupup.com/web/rq/ait/images/share.png`,
      query: '分享',

    }
  }
})